#include "vd_net.h"

size_t vd_udp_send(struct socket *sock, const void *buff, size_t length)
{
    struct msghdr msg;
    struct iovec iov;
    mm_segment_t oldfs;
    int size = 0;

    if (sock->sk==NULL)
       return 0;

    iov.iov_base = (void *)buff;
    iov.iov_len = length;

    msg.msg_flags = 0;
    msg.msg_name = NULL;
    msg.msg_namelen  = 0;
    msg.msg_control = NULL;
    msg.msg_controllen = 0;
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;
    msg.msg_control = NULL;

    oldfs = get_fs();
    set_fs(KERNEL_DS);
    size = sock_sendmsg(sock, &msg, length);
    set_fs(oldfs);

    return size;
}

size_t vd_udp_recv(struct socket *sock, void *buff, size_t length)
{
    struct msghdr msg;
    struct iovec iov;
    mm_segment_t oldfs;
    int size;

    if (sock->sk == NULL)
      return 0;

    iov.iov_base = (void *)buff;
    iov.iov_len = (__kernel_size_t)length;

    msg.msg_flags = 0;
    msg.msg_name = NULL;
    msg.msg_namelen = 0;
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;
    msg.msg_control = NULL;
    msg.msg_controllen = 0;

    oldfs = get_fs();
    set_fs(KERNEL_DS);
    size = sock_recvmsg(sock, &msg, length, msg.msg_flags);
    set_fs(oldfs);

    return size;
}
